//插入资产采购
function insertpurchase_CA(token){
  wx.login(
    {
      success(res) {
   
        if (res.code) {
         console.log ("开始插入采购计划:"+getApp().globalData.token)    
           
          wx.request ({
            url: 'https://2000022758.zhangqx.com/insertpurchase_CA', 
           // method:'POST',
          
            data: {
              token: token,
              ptel: 'WWJKLS',
              ptitle:'柯南2021新款',
              pcontent:'我的出售业务',
              pRelavant:'89897',
              pPrice:7.134,
              pCount:2000,
              ptotal:50000, //不知道  pPrice*pCount
              date:'2021/5/1',
              time: '3:45',
              location: '南京',
              ptype:0,
              pCondition:'5'
            },
          
            
            success(res){   
              console.log(res)       
            }
          })
        }
        else {
          console.log('获取信息失败'+res.errMsg)
        }
      }  
    }) 
    
}

function formatTime(time) {
  if (typeof time !== 'number' || time < 0) {
    return time
  }

  var hour = parseInt(time / 3600)
  time = time % 3600
  var minute = parseInt(time / 60)
  time = time % 60
  var second = time

  return ([hour, minute, second]).map(function (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join(':')
}
 
function getDate() {
  var time = new Date()
  var year = time.getFullYear()
  var month = time.getMonth()
  month = month < 10 ? '0' + month : month
  var day = time.getDay()
  day = day < 10 ? '0' + day : day
  return [year, month, day].join('-')
}

function getTime() {
  var time = new Date()
  var hours = time.getHours()
  hours = hours < 10 ? '0' + hours : hours
  var minute = time.getMinutes()
  minute = minute < 10 ? '0' + minute : minute
  var second = time.getSeconds()
  second = second < 10 ? '0' + second : second
  return [hours, minute, second].join(':')
}





function openid(){
  // 登录
  wx.login(
    {
      success(res) {
        if (res.code) {
              //console.log("尝试登录请求onLogin：")
            // console.log (res.code)
          //发送请求
          wx.request ({
            url: 'https://2000022758.zhangqx.com/getopenid', 
            //method:'POST',
            data: {
              code: res.code 
            },
            success(res){
              console.log(res.data) 
            }
          })
        }
        else {
          console.log('登录失败'+res.errMsg)
        }
      }
      
    })
}
//获取token
 function onloginBypromise(){
    //console.log('尝试执行onLogin函数')
  let promise = new Promise( 
    (resolve, reject) => {
    wx.login(
      {
        success(res) {
          if (res.code) {
            //console.log (res.code)      
            wx.request ({
              url: 'https://2000022758.zhangqx.com/onLogin', //method:'POST',
              data: {
                code: res.code 
              },
              //如果访问成功
              success(res){  
                 if (res.data){
                  getApp().globalData.token = res.data;
                  console.log(res.data)
                  resolve(res.data)
                 }
                 else {
                   reject('失败获取token')
                 }
         
              }
            })
          }
          else {
            console.log('登录失败'+res.errMsg)
          }
        }  
      }) 
  })
  return promise
  }// end of function login
  

 //用户上传                       
function upload(lib){
            wx.chooseMessageFile({
                    count: 10,
                    type: 'all',
                    success (res) {
            
                      const tempFilePaths = res.tempFiles 
                      let filepath = tempFilePaths[0].path //本地的临场路径
                      let name = tempFilePaths[0].name
                      console.log(filepath+" uploading files "+name) 

                      wx.cloud.uploadFile({
                            cloudPath: lib+name, // 上传至云端的路径
                            filePath:filepath
                            })
                    .then((res) => {
                            // 返回文件 ID
                          
                            console.log(res.fileID+" __fileID");
                            return res.fileID;
                            });
                    }
                  })
    }
    function algortihm(e,sig){
      let algorithm = e;
      let formula = sig;
      wx.request({
        url:'https://cloud1-2gklks5l2c606d1c-1305337834.ap-shanghai.app.tcloudbase.com/'+algorithm,
        data:{
                formula: formula
        },
        sucess: res => {
               if(res.statusCode = 200) {                      
                  result: res.data 
                  console.log(result)
                  return result                       
                }
        }
  })    
    }

   
    


module.exports = {
  //formatTime: formatTime,
insertpurchase_current:insertpurchase_CA,

getDate: getDate,
getTime: getTime,

  onlogin:onloginBypromise,
  openid: openid,

  
 algorithm:algortihm,
 upload:upload,



}
